/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package entities;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Temporal;
import util.SpeakkerPlaylistUtil;

/**
 *
 * @author Antoine Boulinguez et Shyn-Yuan Cheng
 */
@Entity
@NamedQueries({
    @NamedQuery(name = "Playlist.getById", query = "SELECT p FROM Playlist p WHERE p.id=:id"),
    @NamedQuery(name = "Playlist.getPublicPlaylists", query = "SELECT p FROM Playlist p WHERE p.privatePlaylist=false"),
    @NamedQuery(name = "Playlist.getByOwnerAndTitle", query = "SELECT p FROM Playlist p WHERE p.owner=:owner and p.title=:title")})
public class Playlist implements Serializable {

    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    private String title;
    @ManyToOne
    private UUser owner;
    @Temporal(javax.persistence.TemporalType.DATE)
    private Date creationDate;
    private String description;
    private int totalTime; // in secondes
    private int nbTimesPlayed;
    @OneToMany
    private Collection<Music> musics;
    private Collection<Genre> genres;
    @OneToMany
    private Collection<Comment> comments;
    private boolean privatePlaylist; // Only the owner can view and play it ?
    @ManyToMany
    private Collection<UUser> sharedUsers; // Playlist sharing with friends

    //
    // Constructors
    //
    public Playlist() {
    }

    public Playlist(String title, UUser owner, Date creationDate, String decription, boolean privatePlaylist) {
        this.title = title;
        this.owner = owner;
        this.creationDate = creationDate;
        this.description = decription;
        this.privatePlaylist = privatePlaylist;

        this.totalTime = 0;
        this.nbTimesPlayed = 0;
        this.musics = new ArrayList<Music>();
        this.genres = new ArrayList<Genre>();
        this.comments = new ArrayList<Comment>();
        this.sharedUsers = new ArrayList<UUser>();
    }

    // 
    // Accessors
    //
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public Date getCreationDate() {
        return creationDate;
    }

    public void setCreationDate(Date creationDate) {
        this.creationDate = creationDate;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String decription) {
        this.description = decription;
    }

    public int getTotalTime() {
        return totalTime;
    }

    public void setTotalTime(int totalTime) {
        this.totalTime = totalTime;
    }

    public int getNbTimesPlayed() {
        return nbTimesPlayed;
    }

    public void setNbTimesPlayed(int nbTimesPlayed) {
        this.nbTimesPlayed = nbTimesPlayed;
    }

    public Collection<Music> getMusics() {
        return musics;
    }

    public void setMusics(Collection<Music> musics) {
        this.musics = musics;
    }

    public Collection<Genre> getGenres() {
        return genres;
    }

    public void setGenres(Collection<Genre> genres) {
        this.genres = genres;
    }

    public Collection<Comment> getComments() {
        return comments;
    }

    public void setComments(Collection<Comment> comments) {
        this.comments = comments;
    }

    public boolean isPrivatePlaylist() {
        return privatePlaylist;
    }

    public void setPrivatePlaylist(boolean privatePlaylist) {
        this.privatePlaylist = privatePlaylist;
    }

    public Collection<UUser> getSharedUsers() {
        return sharedUsers;
    }

    public void setSharedUsers(Collection<UUser> sharedUsers) {
        this.sharedUsers = sharedUsers;
    }

    public UUser getOwner() {
        return owner;
    }

    public void setOwner(UUser owner) {
        this.owner = owner;
    }

    public void addSong(Music music) {
        // If the music isn't in the playlist yet
        if (!musics.contains(music)) {
            musics.add(music);

            // Add genre
            Genre genre = music.getGenre();
            if (!genres.contains(genre)) {
                genres.add(genre);
            }
        }
    }
    
    public boolean addSharedUser(UUser user) {
        if(!sharedUsers.contains(user)) {
            return sharedUsers.add(user);
        }
        
        return false;
    }

    public String getTotalTimeStr() {
        String minutes = String.valueOf(totalTime / 60);
        String secondes = String.valueOf(totalTime % 60);

        // Format
        if (minutes.length() < 2) {
            minutes = "0" + minutes;
        }
        if (secondes.length() < 2) {
            secondes = "0" + secondes;
        }

        return minutes + ":" + secondes;
    }

    public int getNbMusics() {
        return musics.size();
    }

    // Get the playlist under JSon format supported by the javascript Speakker player
    public String getJSon() {
        return SpeakkerPlaylistUtil.getJson(musics);

        /*
         StringBuilder sb = new StringBuilder();
         sb.append("{\"playlist\": [");
        
         int count = 0;
        
         for(Music m : musics ) {
         if(count > 0) {
         sb.append(",");
         }
            
         sb.append("{");
         sb.append("\"");
         sb.append(count);
         sb.append("\": {\"src\":\"");
         sb.append(m.getFilePath());
         sb.append("\", \"type\":\"audio/mp3\"},");
         sb.append("\"config\": ");
         sb.append("{");
         sb.append("\"title\": \"");
         sb.append(m.getTitle());
         sb.append("\"}");
         sb.append("}");
            
         count++;
         }
        
         sb.append("]}");
        
         System.out.println(sb.toString());
        
         return sb.toString();
         */
    }

    public void incPlayCount() {
        nbTimesPlayed++;
    }
    
    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Playlist)) {
            return false;
        }
        Playlist other = (Playlist) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "entities.Playlist[ id=" + id + " ]";
    }
}
